﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EWDTAssignment.App_Code;

namespace EWDTAssignment
{
    public partial class RetrieveSets : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //REMEMBER TO DELETE
            Session["AccountID"] = 1;
            Session["AccType"] = "customer";

            if (!IsPostBack)
            {
                if (Session["AccountID"] != null)
                {
                    btnDel.Attributes.Add("onclick", "return confirm('Are you sure you wish to delete this set? Deleted sets are not recoverable.');");
                    int accID = Convert.ToInt32(Session["AccountID"]);
                    ddlSet1.Visible = false;
                    lblSearch.Visible = false;
                    txtbxSetSearch.Visible = false;
                    btnSearch.Visible = false;
                    btnEdit.Visible = false;
                    btnDel.Visible = false;

                    if (Session["AccType"].ToString().ToLower() == "customer")
                    {
                        BusinessLogic.populateSetList(ddlSet1, accID);
                        ddlSet1.Visible = true;

                        if (Request.UrlReferrer != null)
                        {
                            if (Request.UrlReferrer.LocalPath.ToString() == "/Customize.aspx")
                            {
                                if ((int)Session["createdSetID"] != -1)
                                {
                                    ddlSet1.SelectedValue = Session["createdSetID"].ToString();
                                    BusinessLogic.GenerateSetList((int)Session["createdSetID"], BusinessLogic.CreateLabelList(SetID1, MB1, CPU1, RAM1, GFX1, HDD1, Sound1, PSU1, Casing1));
                                    btnEdit.Visible = true;
                                    btnDel.Visible = true;
                                    Session.Remove("createdSetID");
                                }
                            }
                        }
                    }
                    else
                    {
                        if (Session["AccType"].ToString().ToLower() == "staff")
                        {
                            lblSearch.Visible = true;
                            txtbxSetSearch.Visible = true;
                            btnSearch.Visible = true;
                        }
                    }
                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
            }
        }



        protected void ddlSet1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BusinessLogic.GenerateSetList(Convert.ToInt32(ddlSet1.SelectedValue), BusinessLogic.CreateLabelList(SetID1, MB1, CPU1, RAM1, GFX1, HDD1, Sound1, PSU1, Casing1));
            btnEdit.Visible = true;
            btnDel.Visible = true;
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            int selectedSet = Convert.ToInt32(SetID1.Text.Replace("Set #", ""));
            EntityLogic.DeleteSet(selectedSet);
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int selectEditSet = Convert.ToInt32(SetID1.Text.Replace("Set #", ""));
            Session["selectEditSet"] = selectEditSet;
            Response.Redirect("Customize.aspx?Action=Edit");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int setID = Convert.ToInt32(txtbxSetSearch.Text);
            BusinessLogic.GenerateSetList(Convert.ToInt32(setID), BusinessLogic.CreateLabelList(SetID1, MB1, CPU1, RAM1, GFX1, HDD1, Sound1, PSU1, Casing1), lblError);

            if (SetID1.Text != "")
            {
                btnEdit.Visible = true;
                btnDel.Visible = true;
            }
            else
            {
                btnEdit.Visible = false;
                btnDel.Visible = false;
            }
        }
    }
}